package com.campus.campusdelivery.repository;

import com.campus.campusdelivery.entity.Merchant;
import com.campus.campusdelivery.constant.MerchantStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface MerchantRepository extends JpaRepository<Merchant, Long> {
    
    Optional<Merchant> findByUserId(Long userId);
    
    List<Merchant> findByStatus(MerchantStatus status);
    
    // 新增：带分页的状态查询
    Page<Merchant> findByStatus(MerchantStatus status, Pageable pageable);
    
    List<Merchant> findByIsOpen(Boolean isOpen);
    
    @Query("SELECT m FROM Merchant m WHERE m.status = :status AND m.isOpen = true")
    List<Merchant> findOpenMerchantsByStatus(@Param("status") MerchantStatus status);
    
    @Query("SELECT m FROM Merchant m WHERE m.shopName LIKE %:keyword% OR m.description LIKE %:keyword%")
    Page<Merchant> findByKeyword(@Param("keyword") String keyword, Pageable pageable);
    
    @Query("SELECT COUNT(m) FROM Merchant m WHERE m.status = :status")
    long countByStatus(@Param("status") MerchantStatus status);
    
    @Query("SELECT COUNT(m) FROM Merchant m WHERE m.isOpen = true")
    long countOpenMerchants();
}
